﻿using LoadTestReport.DAO;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LoadTestReport
{
    public partial class SelectCounters : Form
    {
        public SelectCounters()
        {
            InitializeComponent();
            ArrayList Categories = DataReader.getCounterCategories(TestParent.selectedRunID);
            for (int i = 0; i < Categories.Count; i++)
            {
                String CategoryName = Categories[i].ToString();

                ArrayList Counters = DataReader.getAllCounters_for_Category(TestParent.selectedRunID, CategoryName);
                
                bool parentSelect = true;

                TreeNode treeNode = new TreeNode(CategoryName);
                treeView1.Nodes.Add(treeNode);
                for (int j = 0; j < Counters.Count; j++)
                {
                    TreeNode childNode = new TreeNode(Counters[j].ToString());
                    
                    

                    String Category_Counter = CategoryName + "@" + childNode.Text;
                    bool shouldCreate = false;
                    try
                    {
                        if (TestParent.counterState.ContainsKey(Category_Counter))
                        {
                            shouldCreate = TestParent.counterState[Category_Counter];
                            parentSelect = parentSelect && shouldCreate;
                        }
                        else
                        {
                            parentSelect = false;
                        }
                    }
                    catch { }
                    if (shouldCreate) { childNode.Checked = true; }                  
                    treeNode.Nodes.Add(childNode);
                }
                if (parentSelect) { treeNode.Checked = true; }
            }            
            treeView1.AfterCheck += treeView1_AfterCheck;
        }

        void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
        {
            TreeNode selectedNode = (TreeNode)e.Node;
            bool state = selectedNode.Checked;
            TreeNode parentNode = selectedNode.Parent;
            if (parentNode == null)
            {
                for (int i = 0; i < selectedNode.Nodes.Count; i++)
                {
                    selectedNode.Nodes[i].Checked = state;
                }
            }
        }

        private void OKButtonClicked(object sender, EventArgs e)
        {
            for(int i = 0 ; i < treeView1.Nodes.Count ; i++)
            {
                TreeNode TN = treeView1.Nodes[i];
                for(int j = 0 ; j < TN.Nodes.Count ; j++)
                {
                    String counterName = TN.Nodes[j].Text;
                    String parentText = TN.Nodes[j].Parent.Text;
                    Boolean state = TN.Nodes[j].Checked;
                    if (!TestParent.counterState.ContainsKey(parentText + "@" + counterName))
                    {
                        TestParent.counterState.Add(parentText + "@" + counterName, state);
                    }
                    else
                    {
                        TestParent.counterState.Remove(parentText + "@" + counterName);
                        TestParent.counterState.Add(parentText + "@" + counterName, state);
                    }                    
                }
            }
            this.Close();
        }

        private void SelectAllClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            for (int i = 0; i < treeView1.Nodes.Count; i++)
            {
                treeView1.Nodes[i].Checked = true;
            }
        }

        private void deselectClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            for (int i = 0; i < treeView1.Nodes.Count; i++)
            {
                treeView1.Nodes[i].Checked = false;
            }
        }
    }
}
